package com.zygh.questionnairesurvey.controller;

import com.alibaba.fastjson.JSONObject;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.UserInfo;
import com.zygh.questionnairesurvey.entity.vo.UserPasswordVo;
import com.zygh.questionnairesurvey.entity.vo.UserUpdateVO;
import com.zygh.questionnairesurvey.server.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/User")
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "查询所有人员信息")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "民警名称", dataType = "String"),
            @ApiImplicitParam(name = "termStatus", value = "用户类型（0：民警  1：领导班子   3：工勤人员）", dataType = "Integer"),
            @ApiImplicitParam(name = "deptId", value = "部门id", dataType = "Integer"),
            @ApiImplicitParam(name = "warningsignal", value = "警号", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", required = true)
    })
    public CommonResult listAll(String name, Integer termStatus, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                Integer deptId, String warningsignal) {
        return userService.listAll(name, termStatus, deptId, warningsignal, pageNum, pageSize);
    }


    @ApiOperation(value = "根据id查询人员")
    @GetMapping("getById")
    public CommonResult getById(Integer id) {
        return userService.getById(id);
    }

    @ApiOperation(value = "新增人员信息")
    @PostMapping("insert")
    public CommonResult insert(@RequestBody UserInfo userInfo) {
        return userService.insert(userInfo);
    }

    @ApiOperation(value = "修改人員信息")
    @PostMapping("update")
    public CommonResult update(@RequestBody UserInfo userInfo) {
        return userService.update(userInfo);
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除人员信息")
    public CommonResult delete(@RequestBody String id) {
        String str = JSONObject.parseObject(id).get("id").toString();
        Integer new_id = Integer.parseInt(str);
        return userService.delete(new_id);
    }

    @GetMapping("findUserByQuest")
    @ApiOperation(value = "查询没有填写调查问卷的民警")
    public CommonResult findUserByQuest(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                        Integer deptId, String warningsignal, String name) {
        return userService.findUserByQuest(pageNum, pageSize, deptId, warningsignal, name);
    }

    @GetMapping("findUserInfoById")
    @ApiOperation(value = "根据部门和警号查询人员列表，和问题个数")
    public CommonResult findUserInfoById(String deptId, String warningsignal, String name,
                                         @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.findUserInfoById(deptId, warningsignal, name, pageNum, pageSize);
    }

    @GetMapping("findUserByProbelm")
    @ApiOperation(value = "根据问题id查询人员信息")
    public CommonResult findUserByProbelm(Integer id,
                                          @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.findUserByProbelm(id, pageNum, pageSize);
    }

    @GetMapping("findUserByNum")
    @ApiOperation(value = "查询已填问卷人数和已填问卷人数")
    public CommonResult findUserByNum(Integer deptId) {
        return userService.findUserByNum(deptId);
    }


    @PostMapping("/updatePass")
    @ApiOperation(value = "修改密码")
    public CommonResult updatePass(@RequestBody UserPasswordVo userPassword) {
        return userService.updatePass(userPassword);
    }

    @PostMapping("/resetPasswords")
    @ApiOperation(value = "重置密码")
    public CommonResult resetPasswords(@RequestBody String username) {
        String str = JSONObject.parseObject(username).get("username").toString();
        return userService.resetPasswords(str);
    }


    @PostMapping("updateBase")
    @ApiOperation(value = "批量修改用户部门")
    public CommonResult updateBase(@RequestBody UserUpdateVO list) {
        return userService.updateBase(list);
    }

    @GetMapping("/findUserAdmin")
    @ApiOperation(value = "查询所有管理员")
    public CommonResult findUserAdmin(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.findUserAdmin(pageNum, pageSize);
    }

    @GetMapping("/findUserCountByid")
    @ApiOperation(value = "根据部门id查询该部门民警，领导，工勤人员各多少人")
    public CommonResult findUserCountByid(Integer id) {
        return userService.findUserCountByid(id);
    }

    @GetMapping("/findUserByProblem")
    @ApiOperation(value = "根据问题id和部门id查询人员信息")
    public CommonResult findUserByProblem(Integer deptId, String encoder, Integer termStatus, String warningsignal, String name,
                                          @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return userService.findUserByProblem(deptId, encoder, termStatus, warningsignal, name, pageNum, pageSize);
    }


}